import { Entity, PrimaryGeneratedColumn, Column, OneToMany, CreateDateColumn, ManyToMany, JoinTable } from "typeorm";
import { ApiProperty } from "@nestjs/swagger";

import { UserEntity} from './../user/user.entity'
import {MenuEntity} from './../menu/menu.entity'

@Entity({name:'tyk_role'})
export class RoleEntity{
    @PrimaryGeneratedColumn({name:'role_id'})
    roleId:number

    @ApiProperty({description:'角色名称'})
    @Column({name:'role_name'})
    roleName:string

    @ApiProperty({description:'角色备注'})
    @Column({name:'role_remark',default:'无备注'})
    roleRemark:string

    @ManyToMany(
        type=>UserEntity,
        users=>users.roles,
    )
    users:UserEntity[]
    
    @ManyToMany(
        type=>MenuEntity,
        menus=>menus.roles,
        { cascade: ['insert', 'remove'], nullable: false },   
    )
    @JoinTable({
        name: 'tyk_role_menu',
        joinColumns: [
          {name: 'role_id'},
        ],
        inverseJoinColumns: [
          {name: 'menu_id'},
        ],
      })
    menus:MenuEntity[]

    @ApiProperty({description:'创建时间'})
    @CreateDateColumn({ type: 'timestamp', name: 'create_date', comment: '创建时间' })
    createDate: Date
}